<?php
declare (strict_types = 1);

namespace app\index\controller;
use app\common\service\AdminDianliang as S;
use app\common\service\AdminDlrecord as RS;
use think\facade\Request;

class Index extends \app\BaseController
{
    /**
     * 首页
     */
    //protected $middleware = ['WxCheck'];
    public function index()
    {
        header('X-Frame-Options:ALLOW-FROM http://xf.xingtaiwang.net');
        $dllist=S::getselect();
      
        return $this->fetch('index',['dllist'=>$dllist["data"],'cuser'=>get_curuser()]);
    }
    public function testwx(){
        
    }

    public function dl()
    {
        $check = Request::checkToken('__token__');
        if(false === $check) {
            return $this->getJson(['msg' => '验证失败', 'code' => 204]);
        }
        $user_agente = $_SERVER['HTTP_USER_AGENT'];
        if(Request::isAjax())
        {
            //判断微信
            if (strpos($user_agente, 'MicroMessenger') == false) {die; }
            //判断是否当前域名下ntp.diandianx.com
            if(!isset($_SERVER['HTTP_REFERER']) || !stripos($_SERVER['HTTP_REFERER'],'xf.xingtaiwang.net')) {die;}
           
            if(!Request::isGet()){die;}
            $openid=get_curuser()["openid"];
            $time1=date("Y-m-d H:i:s");                              
            $time2="2022-02-16 10:00:00";  
            $can=betweenTime($time1,$time2);
            if($can){
            return $this->getJson(RS::dl($openid,Request::param("qxid")));
            }
            else{ return $this->getJson(['msg' => '活动已结束，感谢参与！', 'code' => 204]);}
        }
        $dllist=S::getselect();
        $dlstr="";
        foreach($dllist['data'] as $item)
        {
            $dlstr.='{"title":"'.$item["qxname"].'","value":"'.$item["id"].'"}'.',';
        }
        //$dllist=$this->getJson($dllist);
        return $this->fetch('dl',['dlstr'=>$dlstr,'cuser'=>get_curuser()]);
    }
    public function dlrank()
    {
        if (Request::isAjax()) {
            return $this->getJson(S::getrank());
         }
        return $this->fetch('dlrank',['cuser'=>get_curuser()]);
    }
}
